package com.example.opencvtest;

import java.io.IOException;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
import autotrace.Autotrace;

public class MainActivity extends Activity
{
	public static final String	TAG				= "OCVTest::MainActivity";
	CameraPreviewView			mTestSurfaceView;

	boolean						mOpenCvLoaded	= false;

	static {
		// load the native library at program startup
		System.loadLibrary("autotrace");
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.i(TAG, "onCreate");
	}

	private void onOpenCvLoaded()
	{
		Log.i(TAG, "OpenCV loaded successfully");

		setContentView(R.layout.activity_main);
		mTestSurfaceView = (CameraPreviewView) findViewById(R.id.cameraPreviewView1);

		OverlayView overlay = new OverlayView(getApplicationContext());
		FrameLayout frameLayout1 = (FrameLayout) findViewById(R.id.frameLayout1);
		frameLayout1.addView(overlay);
	

		mTestSurfaceView.setOverlay(overlay);

		Button b = (Button) findViewById(R.id.button1);
		b.setOnClickListener(new OnClickListener()
		{

			public void onClick(View v)
			{
				mTestSurfaceView.takeTheShot();
			}
		});

		SeekBar sbLower = (SeekBar) findViewById(R.id.seekBar1);
		sbLower.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
		{

			public void onStopTrackingTouch(SeekBar seekBar)
			{
			}

			public void onStartTrackingTouch(SeekBar seekBar)
			{
			}

			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
			{
				mTestSurfaceView.mCannyLower = (int) (progress * 2.55);
			}
		});

		SeekBar sbHigher = (SeekBar) findViewById(R.id.seekBar2);
		sbHigher.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
		{

			public void onStopTrackingTouch(SeekBar seekBar)
			{
			}

			public void onStartTrackingTouch(SeekBar seekBar)
			{
			}

			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
			{
				mTestSurfaceView.mCannyHigher = (int) (progress * 2.55);
			}
		});

		SeekBar sb3 = (SeekBar) findViewById(R.id.seekBar3);
		sb3.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
		{

			public void onStopTrackingTouch(SeekBar seekBar)
			{
			}

			public void onStartTrackingTouch(SeekBar seekBar)
			{
			}

			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
			{
				mTestSurfaceView.mSb3Value = progress;
			}
		});

	}

	private BaseLoaderCallback	mOpenCVCallBack	= new BaseLoaderCallback(this)
	{
		@Override
		public void onManagerConnected(int status)
		{
			Log.i(TAG, "onManagerConnected");
			switch (status) {
				case LoaderCallbackInterface.SUCCESS: {
					mOpenCvLoaded = true;
					onOpenCvLoaded();
				}
					break;
				default: {
					Log.i(TAG, "OpenCV not loaded status: " + status);
					super.onManagerConnected(status);
					break;
				}

			}
		}
	};

	@Override
	protected void onStop()
	{
		Log.i(TAG, "onStop");
		super.onStop();
		mTestSurfaceView.disableView();
	};

	@Override
	protected void onPause()
	{
		super.onPause();
		Log.i(TAG, "onPause");
	};

	@Override
	protected void onResume()
	{
		super.onResume();
		Log.i(TAG, "called onResume");

		if (!mOpenCvLoaded) {
			Log.i(TAG, "Trying to load OpenCV library");

			if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mOpenCVCallBack)) {
				Log.e(TAG, "Cannot connect to OpenCV Manager");
			
			}
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		Log.i(TAG, "onCreateOptionsMenu");
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}
